package com.jmb.waimao.utils;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.chromium.ChromiumOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;
import java.util.List;

public class FacebookSelenium {
    public static void main(String[] args) throws InterruptedException {
        // 配置 Chrome 选项
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--start-maximized");
        options.addArguments("--disable-blink-features=AutomationControlled");

        WebDriver driver = new ChromeDriver(options);

        try {
            // 1. 登录 Facebook
            driver.get("https://www.facebook.com");
            WebElement email = driver.findElement(By.ID, "email");
            WebElement password = driver.findElement(By.ID, "pass");

            email.sendKeys("your_email@example.com");
            password.sendKeys("your_password");
            driver.findElement(By.NAME, "login").click();

            // 2. 处理登录后的验证（可能需要手动操作）
            Thread.sleep(20000); // 等待手动处理验证码

            // 3. 访问好友列表
            driver.get("https://www.facebook.com/me/friends");
            WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(20));

            // 等待好友列表加载
            wait.until(ExpectedConditions.presenceOfElementLocated(
                    By.CSS_SELECTOR, "[data-testid='friend_list_item']"));

            // 4. 解析好友列表
            List<WebElement> friends = driver.findElements(
                    By.CSS_SELECTOR, "[data-testid='friend_list_item']");

            System.out.println("找到好友数量: " + friends.size());
            for (int i = 0; i < Math.min(5, friends.size()); i++) { // 示例显示前5个
                String name = friends.get(i).findElement(
                        By.CSS_SELECTOR, "span[data-testid='friend_name']").getText();
                System.out.println((i+1) + ". " + name);
            }

        } finally {
            driver.quit();
        }
    }
}
